module.exports = function WS(url) {
	//地址url
	this.url = url;
	//作为get获取数据的回调对象存储
	this.messageList = {};

	//socket对象
	this.socket = null;

	//自定义Ws连接函数：服务器连接成功
	this.onOpen = (e) => {
		console.log(e)
	};
	//自定义Ws消息接收函数：服务器向前端推送消息时触发
	this.onMessage = (e) => {
		//处理各种推送消息
		this.handleEvent(e)
	};
	//自定义Ws异常事件：Ws报错后触发
	this.onError = (e) => {
		console.log('error');
	};
	//自定义Ws关闭事件：Ws连接关闭后触发
	this.onClose = (e) => {
		console.log('close')
	};
	//初始化websocket连接
	this.initWs()
};
//初始化websocket连接
module.exports.prototype.initWs = function() {
	window.WebSocket = window.WebSocket;
	if (!window.WebSocket) { // 检测浏览器支持
		console.error('错误: 浏览器不支持websocket');
		return;
	}
	let that = this;
	this.socket = new WebSocket(this.url); // 创建连接并注册响应函数
	this.socket.onopen = (e) => {
		that.onOpen(e);
	};
	this.socket.onmessage = (e) => {
		that.onMessage(e);
	};
	this.socket.onclose = (e) => {
		that.onClose(e);
		that.socket = null; // 清理
	};
	this.socket.onerror = (e) => {
		that.onError(e);
	};
	return this;
};


module.exports.prototype.reConnect = function() {
	if (this.isReconnect) return;
	this.isReconnect = true;
	//没连接上会一直重连，设置延迟避免请求过多
	setTimeout(function() {
		this.initWs();
		this.isReconnect = false;
	}, 2000);
};

// WS.prototype.timestamp = null;

//处理消息
module.exports.prototype.handleEvent = function(e) {
	const response = JSON.parse(e.data);
	// console.log(response);
	let tempKey = response.wid + "-" + response.baseOrApp + "-" + response.msgId;
	if (this.messageList[tempKey]) {
		this.messageList[tempKey](response); //去除合成唯一id 执行回调
	} else { //主动上报消息,在messageList无key
		this.handleAction[response.wid](response, this)
	}

};

module.exports.prototype.handlerTagEpcLog = function(res) {};
module.exports.prototype.handlerTagEpcOver = function(res) {};
module.exports.prototype.handlerTag6bLog = function(res) {};
module.exports.prototype.handlerTag6bOver = function(res) {};
module.exports.prototype.handlerTagGbLog = function(res) {};
module.exports.prototype.handlerTagGbOver = function(res) {};
module.exports.prototype.handlerGpiStart = function(res) {};
module.exports.prototype.handlerGpiOver = function(res) {};

module.exports.prototype.handleAction = {
	5: function(res, _this) { //epc标签上报
		_this.handlerTagEpcLog(res);
	},
	6: function(res, _this) { //epc停止上报
		_this.handlerTagEpcOver(res);
	},
	7: function(res, _this) { //6c标签上报
		_this.handlerTag6bLog(res);
	},
	8: function(res, _this) { //6b停止上报
		_this.handlerTag6bOver(res);
	},
	9: function(res, _this) { //gb标签上报
		_this.handlerTagGbLog(res);
	},
	10: function(res, _this) { //gb停止上报
		_this.handlerTagGbOver(res);
	},
	11: function(res, _this) { //tcp断链回调
		_this.handlerTcpDisconnected(res);
	},
	12: function(res, _this) { //GPI触发上报
		_this.handlerGpiStart(res);
	},
	13: function(res, _this) { //GPI触发上报结束
		_this.handlerGpiOver(res);
	},
};

//发送消息后回调
module.exports.prototype.sendMsg = function(msg, callback) {
	//存储事件
	let tempKey = msg.wid + "-" + msg.baseOrApp + "-" + msg.msgId; //合成发送事件唯一ID
	this.messageList[tempKey] = callback;
	this.socket.send(JSON.stringify(msg));
};

module.exports.prototype.getUID = function() { // 获取唯一值
	return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
		let r = Math.random() * 16 | 0,
			v = c == 'x' ? r : (r & 0x3 | 0x8);
		return v.toString(16);
	});
};
